package cn.tedu._02notice.controller;

import cn.tedu._02notice.base.response.JsonResult;
import cn.tedu._02notice.base.response.StatusCode;
import cn.tedu._02notice.mapper.NoticeMapper;
import cn.tedu._02notice.pojo.dto.NoticeAddParam;
import cn.tedu._02notice.pojo.dto.NoticeAddQuery;
import cn.tedu._02notice.pojo.dto.NoticeUpdateParam;
import cn.tedu._02notice.pojo.entity.Notice;
import cn.tedu._02notice.pojo.vo.NoticeListVO;
import cn.tedu._02notice.pojo.vo.NoticeListInfoVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * RestController注解：
 * 1.组合注解，相当于Controller+ResponseBody
 * 2.类上添加该注解后，所有的控制器方法无需添加@ResponseBody注解
 * slf4j注解：
 *  1.Lombok提供的日志注解
 *  2.添加在类上，会自动生成一个日志对象log
 *  3.log对象的方法
 *    3.1 log.trace()：打印一条跟踪信息
 *    3.2 log.debug()：打印一条调试信息
 *    3.3 log.info()：打印一条信息
 *    3.4 log.warn()：打印一条警告信息
 *    3.5 log.error()：打印一条错误信息
 *
 * Knife4j(小刀)注解：
 *    1.@Tag：调整控制器类名在Knife4j中显示的类名
 *    2.@Operation：调整控制器方法名在Knife4j中显示的方法名
 *    3.@ApiOperationSupport：调整控制器方法在Knife4j中显示的顺序
 *    4.@Parameter：调整控制器方法参数在Knife4j中显示的参数名称
 *      @Parameters:调整控制器方法参数在Knife4j中显示的参数名称
 */
//@Controller
@Tag(name="01-资讯管理模块")
@Slf4j
@RestController
@RequestMapping("/v1/notice")

public class NoticeController {
    @Autowired
    private NoticeMapper noticeMapper;

    /**
     * 1.添加资讯功能
     */
    @PostMapping("add")
    @Operation(summary = "1.添加资讯")
    @ApiOperationSupport(order = 10)
    @Parameters(value = {
        @Parameter(name = "title", description = "资讯标题", required = true, example = "这是标题"),
        @Parameter(name = "content", description = "资讯内容", required = true, example = "这是内容"),
        @Parameter(name = "type", description = "资讯类型", required = true, example = "1"),
        @Parameter(name = "status", description = "资讯状态", required = true, example = "1"),
        @Parameter(name = "noticeAddParam",hidden = true)
    })
    //@ResponseBody
    public JsonResult add(NoticeAddParam noticeAddParam) {
        log.debug("noticeAddParam = " + noticeAddParam);
        //执行插入数据的逻辑
        //目标：将资讯信息添加到数据库的notice表中
        Notice notice = new Notice();
//        notice.setTitle(noticeAddParam.getTitle());
//        notice.setContent(noticeAddParam.getContent());
//        notice.setType(noticeAddParam.getType());
//        notice.setStatus(noticeAddParam.getStatus());
        BeanUtils.copyProperties(noticeAddParam, notice); //
        //处理其他属性
        notice.setUserId(1L);
        notice.setCreateTime(new Date());
        notice.setUpdateTime(new Date());
        log.debug("notice = " + notice);
        //调用接口层方法
        noticeMapper.insertNotice(notice);
        //return new JsonResult(2000, "添加资讯成功");
        return JsonResult.ok();
    }

    /**
     * 2.资讯列表功能
     */
    @GetMapping("list")
    @Operation(summary = "2.资讯列表")
    @ApiOperationSupport(order = 20)
    @Parameters(value = {
        @Parameter(name = "title", description = "资讯标题", required = true, example = "这是标题"),
        @Parameter(name = "type", description = "用户名", required = true, example = "1"),
        @Parameter(name = "noticeAddQuery",hidden = true)})
    //@ResponseBody
    public JsonResult list(NoticeAddQuery noticeAddQuery) {
        log.debug("noticeAddQuery = " + noticeAddQuery);
        List<NoticeListVO> noticeListVO = noticeMapper.selectNoticeList(noticeAddQuery);
        //调用接口层方法
        return JsonResult.ok(noticeListVO);
    }
    /**
     * 资讯详情功能
     */
    @GetMapping("detail")
    @Operation(summary = "3.资讯详情")
    @ApiOperationSupport(order = 30)
    @Parameters(value = {@Parameter(name = "id", description = "资讯编号", required = true, example = "1"),
            @Parameter(name = "username", description = "资讯编号", required = true, example = "1")})
    //@ResponseBody
    public JsonResult detail(Long id,String username) {
        log.debug("id = " + id);
        NoticeListInfoVO noticeInfoVO = noticeMapper.selectNoticeDetail(id);
        return JsonResult.ok(noticeInfoVO);
    }

    /**
     * 删除资讯功能
     */
    @DeleteMapping("del")
    @Operation(summary = "4.删除资讯")
    @ApiOperationSupport(order = 40)
    @Parameter(name = "id", description = "资讯编号", required = true, example = "1")
    //@ResponseBody
    public JsonResult del(Long id) {
        log.debug("id = " + id);
        int count =noticeMapper.deleteNotice(id);
        if (count > 0) {
            return JsonResult.ok();
        }
        return new JsonResult(StatusCode.OPERYION_FAIL);
    }

    /**
     * 更新资讯
     */
    @PostMapping("update")
    @Operation(summary = "5.更新资讯")
    @ApiOperationSupport(order = 50)
    @Parameters(value = {
        @Parameter(name = "id", description = "资讯编号", required = true, example = "1"),
        @Parameter(name = "title", description = "资讯标题", required = true, example = "这是标题"),
        @Parameter(name = "content", description = "资讯内容", required = true, example = "这是内容"),
        @Parameter(name = "type", description = "资讯类型", required = true, example = "1"),
        @Parameter(name = "status", description = "资讯状态", required = true, example = "1"),
        @Parameter(name = "noticeUpdateParam",hidden = true)})
    //@ResponseBody
    public JsonResult update(NoticeUpdateParam noticeUpdateParam) {
        log.debug("noticeUpdateParam = " + noticeUpdateParam);
        Notice notice = new Notice();
        BeanUtils.copyProperties(noticeUpdateParam, notice);
        //处理更新时间
        notice.setUpdateTime(new Date());
        //调用接口
        int count = noticeMapper.updateNotice(notice);
        if (count > 0){
            return JsonResult.ok();
        }
        return new JsonResult(StatusCode.OPERYION_FAIL);
    }

}
